package com.mazaiting.redeye.service;

import com.mazaiting.redeye.base.Base;
import com.mazaiting.redeye.config.GlobalConfig;
import com.mazaiting.redeye.domain.Response;
import com.mazaiting.redeye.domain.ResponseCode;
import com.mazaiting.redeye.domain.WormBean;
import com.mazaiting.redeye.utils.net.HttpUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;

import java.util.List;

/***
 *
 *
 *                                                    __----~~~~~~~~~~~------___
 *                                   .  .   ~~//====......          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
 * @author mazaiting
 * @date 2019-07-05
 * @decription 图虫服务
 */
@Service
public class WormService extends Base {

    /**
     * 获取图片信息列表
     * @return 偏好列表
     */
    public Response<List<WormBean.FeedBean>> feed() {
        // 创建响应
        Response<List<WormBean.FeedBean>> response = new Response<>();
        try {
            // 获取图虫数据
            WormBean wormBean = HttpUtil.get(GlobalConfig.URL_WORM, WormBean.class);
            if (null != wormBean) {
                // 获取数据列表
                List<WormBean.FeedBean> feedList = wormBean.getFeedList();
//                // 迭代
//                feedList.forEach(feedBean -> {
//                    mLog.debug(feedBean.toJson());
//                    List<WormBean.ImageBean> images = feedBean.getImages();
//                    images.forEach(imageBean -> {
//                        mLog.debug(imageBean.toJson());
//                    });
//                    WormBean.SiteBean site = feedBean.getSite();
//                    mLog.debug(site.toJson());
//                    List<String> tags = feedBean.getTags();
//                    tags.forEach(tag -> {
//                        mLog.debug(tag);
//                    });
//                });
                // 检测数据是否为空
                if (feedList.size() == 0) {
                    response.setResponse(ResponseCode.ERROR, "数据为空");
                } else {
                    response.setResponse(ResponseCode.SUCCESS, "获取成功", feedList);
                }
            }
        } catch (RestClientException e) {
            mLog.debug(e.getMessage());
            response.setResponse(ResponseCode.ERROR, e.getMessage());
        }

        return response;
    }

    @Override
    protected Class<?> getChildClass() {
        return this.getClass();
    }
}
